<?php

namespace app\common\model;

use think\Model;

class Building extends Model
{
    protected $connection = 'db2';
    // 表名
    protected $name = 'building';
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    // 定义时间戳字段名
    protected $createTime = 'created_at';
    protected $updateTime = false;

    const TEACH = 1;
    const DORM = 2;
    const MIX = 3;

    /**
     * @var array 楼栋类型
     */
    public static $typeList = [
        '1' => '教学楼',
        '2' => '寝室楼',
        '3' => '混合楼',
    ];

    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['type'];
        return isset(static::$typeList[$value]) ? static::$typeList[$value] : '';
    }

    public function school()
    {
        return $this->belongsTo('School');
    }

    public function garden()
    {
        return $this->belongsTo('Garden', 'garden_id', 'id', [], 'LEFT')->field('id,name');
    }

    public function classrooms()
    {
        return $this->hasMany('Classroom', 'building_id');
    }

}
